package com.yixiu.javabase.modules.collection;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CollectionInterface   {

    public static void main(String[] args) {
        // set
        //testHashSet();
        //testLinkedHashSet();
        //testTreeSet();

        // list
        //testArrayList();
        //testVector();
        //testLinkedList();

        // queue
        //testQueue();


        // Map
        //testHashtable();
        // testLinkedHashMap();
        // testHashMap();
        //testTreeMap();

        Double [] someData  = { 1.23, 14.67, 12.3, 2.3};  // 不能使用基本类型关键词double
        printArray(someData);

        // 属性类Properties，继承自Hashtable
        Properties properties = null;

        // 栈继承自 Vector
        Stack stack = null;

        // 队列是单独的接口
        Queue queue = null;
        PriorityQueue priorityQueue = null;

    }


    public static void importantInterface() {

        // 集合的根接口
        Collection coll = null;
        List list = null;  // 有序列表，继承自Collection
        Set set = null;   // 无序集合，继承自Collection
        Queue queue = null; // 队列，继承自Collection

        // 键值对集合的根
        Map map = null;

        // 实现Set的具体类
        HashSet hashSet = new HashSet();  // 无序集合，实现了Set接口; 线程安全的集合Collections.synchronizedSet(new HashSet());
        LinkedHashSet linkedHashSet = new LinkedHashSet(); // 继承自HashSet，实现了Set接口
        TreeSet treeSet = new TreeSet(); //  继承顺序 TreeSet > NavigableSet >  SortedSet > Set





        // 实现List的具体类
        ArrayList arrayList = new ArrayList(); // 实现List接口
        Vector vector = new Vector(); // 实现List接口
        LinkedList linkedList = new LinkedList(); // 实现List接口,同时实现接口 Deque > Queue


        PriorityQueue priorityQueue = new PriorityQueue(); // 实现 Queue接口


        // 键值对
        HashMap hashMap = new HashMap();  // 实现了Map接口
        LinkedHashMap linkedHashMap = new LinkedHashMap(); // 有序的Map集合
        ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap(); // 线程安全的hashmap
    }

    public static void testHashSet() {
        HashSet hashSet = new HashSet();
        boolean addFlag = hashSet.add("hello"); // 同一个元素只能添加一次
        System.out.println(addFlag);
        addFlag = hashSet.add("hello");
        System.out.println(addFlag);

        addFlag = hashSet.add(null); // 可以添加null
        System.out.println(addFlag);

        addFlag = hashSet.add(null);
        System.out.println(addFlag);

        addFlag = hashSet.add("hello2");
        addFlag = hashSet.add("hello1");
        addFlag = hashSet.add("hello3");


        for (Object o: hashSet) {
            System.out.println(o);
        }
    }

    public static void testLinkedHashSet() {
        LinkedHashSet linkedHashSet = new LinkedHashSet();
        boolean addFlag = false;
        addFlag = linkedHashSet.add("hello");
        System.out.println(addFlag);
        addFlag = linkedHashSet.add("hello");
        System.out.println(addFlag);
        addFlag = linkedHashSet.add("hello2");
        addFlag = linkedHashSet.add("hello1");
        addFlag = linkedHashSet.add("hello3");

        for (Object o : linkedHashSet) {
            System.out.println(o);
        }

    }

    public  static void testTreeSet() {
        TreeSet treeSet = new TreeSet();
        treeSet.add(new Person("张10",10));
        treeSet.add(new Person("张2",2));
        treeSet.add(new Person("李四",4));
        treeSet.add(new Person("王五",5));
        treeSet.add(new Person("张三",3));
        for(Object o : treeSet) {
            System.out.println(o);
        }
    }


    public static void testArrayList() {
        ArrayList arrayList = new ArrayList();
        Person person = new Person("重复人",100);
        arrayList.add(person);
        arrayList.add(person);
        arrayList.add(null);
        arrayList.add(null);

        arrayList.add(new Person("张10",10));
        arrayList.add(new Person("张2",2));
        arrayList.add(new Person("李四",4));
        arrayList.add(new Person("王五",5));
        arrayList.add(new Person("张三",3));


        for(Object o : arrayList) {
            System.out.println(o);
        }

    }


    public static void testVector() {
        Vector vector = new Vector();

        Person person = new Person("重复人",100);
        vector.add(person);
        vector.add(person);
        vector.add(null);
        vector.add(null);

        vector.add(new Person("张10",10));
        vector.add(new Person("张2",2));
        vector.add(new Person("李四",4));
        vector.add(new Person("王五",5));
        vector.add(new Person("张三",3));


        for(Object o : vector) {
            System.out.println(o);
        }

    }


    public static void testLinkedList() {
        LinkedList linkedList = new LinkedList();


        Person person = new Person("重复人",100);
        linkedList.add(person);
        linkedList.add(person);
        linkedList.add(null);
        linkedList.add(null);

        linkedList.add(new Person("张10",10));
        linkedList.add(new Person("张2",2));
        linkedList.add(new Person("李四",4));
        linkedList.add(new Person("王五",5));
        linkedList.add(new Person("张三",3));


        for(Object o : linkedList) {
            System.out.println(o);
        }

    }

    public  static void testQueue() {
        PriorityQueue priorityQueue = new PriorityQueue();
        // priorityQueue.add(null); // 不能插入null  NullPointerException
        Person person = new Person("重复人",100);

        priorityQueue.add(person);
        priorityQueue.add(person);

        priorityQueue.add(new Person("张10",10));
        priorityQueue.add(new Person("张2",2));
        priorityQueue.add(new Person("李四",4));
        priorityQueue.add(new Person("王五",5));
        priorityQueue.add(new Person("张三",3));

        for(Object o : priorityQueue) {
            System.out.println(o);
        }

        // peek()	读取元素，（不删除）
        // poll()	取出元素，（删除）
        while(!priorityQueue.isEmpty()){
            System.out.println(priorityQueue.poll());  // 用peek就会死循环，一直读取第1个元素
        }


    }


    public static void testHashtable() {
        Hashtable hashtable = new Hashtable();
        hashtable.put("03",new Person("张3",3));
        hashtable.put("09",new Person("张9",9));
        hashtable.put("02",new Person("张2",2));
        hashtable.put("13",new Person("张13",13));
        hashtable.put("03",new Person("张三",33));

        //hashtable.put("00",null); // key和value都不能为null

        for(Object key :  hashtable.keySet()) {
            System.out.println(key.toString() + "  =  " + hashtable.get(key));
        }

    }


    public static void testLinkedHashMap() {
        LinkedHashMap linkedHashMap = new LinkedHashMap();

        linkedHashMap.put("03",new Person("张3",3));
        linkedHashMap.put("09",new Person("张9",9));
        linkedHashMap.put("02",new Person("张2",2));
        linkedHashMap.put("13",new Person("张13",13));
        linkedHashMap.put("03",new Person("张三",33));

        linkedHashMap.put(null,null);  // key和value都能为null


        for(Object key :  linkedHashMap.keySet()) {
            System.out.println(key + "  =  " + linkedHashMap.get(key));
        }

    }

    public static void testHashMap() {
        HashMap hashMap = new HashMap();

        hashMap.put("03",new Person("张3",3));
        hashMap.put("09",new Person("张9",9));
        hashMap.put("02",new Person("张2",2));
        hashMap.put("13",new Person("张13",13));
        hashMap.put("03",new Person("张三",33));

        hashMap.put(null,null);  // key和value都能为null


        for(Object key :  hashMap.keySet()) {
            System.out.println(key + "  =  " + hashMap.get(key));
        }

    }


    public static void testTreeMap() {
        TreeMap treeMap = new TreeMap();

        treeMap.put("03",new Person("张3",3));
        treeMap.put("09",new Person("张9",9));
        treeMap.put("02",new Person("张2",2));
        treeMap.put("13",new Person("张13",13));
        treeMap.put("03",new Person("张三",33));

        treeMap.put("ff",null);  // key不能为null，value可以为null

        for(Object key :  treeMap.keySet()) {
            System.out.println(key + "  =  " + treeMap.get(key));
        }
        System.out.println("子树如下：");
        SortedMap sortedMap =  treeMap.subMap("02","ff");  // 按照key的起始位置截取
        for(Object key :  sortedMap.keySet() ) {
            System.out.println(key + "  =  " + treeMap.get(key));
        }
    }



    // 泛型方法 printArray
    public static <E> void printArray( E[] inputArray )
    {
        // 输出数组元素
        for ( E element : inputArray ){
            System.out.printf( "%s ", element );
        }
        System.out.println();
    }

}


 class Person implements Comparable<Person> {
    private String name;
    private int age;
    public Person(String name,int age) {
        this.name = name;
        this.age = age;
    }
    public int compareTo(Person o) {
        //this指现在插入的元素; o指已经插入的元素,在TreeMap中初始为根节点(从根节点开始比较,一直找到this的位置为止)
        // 遍历输出：从最左侧开始遍历，一直到最右侧。

        // 当compareTo方法返回0,集合中只有第1个添加的元素
        //return 0;

        //当compareTo方法返回正数，表示当前插入的元素比所有节点大，会被插入到最右侧；输出时会按照插入顺序输出
      //return 1;

        //当compareTo方法返回负数，表示当前插入的元素比所有节点小，会被插入到最左侧；输出时会按照插入逆序输出
       //return -1;

        // 按照年龄从小到大排序
        //return  this.age - o.age ;
        // 按照年龄从大到小排序
        return   o.age - this.age  ;
    }
     @Override
     public String toString() {
         return this.name  +  "  "  +  this.age;
     }
 }





